import { Inject, Controller,  Post, Body, Get, Query } from '@midwayjs/decorator';
import { Validate } from '@midwayjs/validate';
import { ControllerBase } from '../base/ControllerBase';
import { ErrCode } from '../common/ErrCode';
import { UserLoginDTO } from '../dto/user.dto';
import { UserEntity } from '../entity/user.entity';
import { UserService } from '../service/user.service';

@Controller('/user')
export class UserController extends ControllerBase {
 
  @Inject()
  userService: UserService;

  @Get('/test')
  async test(@Query() reg:UserLoginDTO):Promise<any>{
    //  return this.ReturnData(await this.userService.registered(reg))
      const code:ErrCode|UserEntity = await this.userService.login(reg);
      if(code == ErrCode.USER_LGIN_FAIL_PWD || code == ErrCode.USER_NOT_FOUND){
        return this.ReturnData(code);
      }else{
        return this.ReturnData(ErrCode.USER_LOGIN_SUCCESS,code);
      }
  }
  @Post('/reg')
  @Validate()
  async registered(@Body() reg:UserLoginDTO):Promise<any>{
      const code:ErrCode = await this.userService.registered(reg);
      return this.ReturnData(code);
  }
  @Post('/login')
  @Validate()
  async login(@Body() logininfo:UserLoginDTO ):Promise<any>{
      const code:ErrCode|UserEntity = await this.userService.login(logininfo);
      if(code == ErrCode.USER_LGIN_FAIL_PWD || code == ErrCode.USER_NOT_FOUND){
        return this.ReturnData(code);
      }else{
        return this.ReturnData(ErrCode.USER_LOGIN_SUCCESS,code);
      }
  }
}
